package com.star.carservice.client.activity;

import butterknife.ButterKnife;
import butterknife.OnClick;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.star.carservice.R;
import com.star.carservice.client.AppController;
import com.star.carservice.client.activity.base.BaseActivity;
import com.star.carservice.client.utils.PreferencesUtils;
import com.star.carservice.client.utils.StringUtils;

import android.content.Intent;
import android.graphics.PointF;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class SplashActivity extends BaseActivity implements BDLocationListener {

	private final static String TAG = "SplashActivity";

	private LocationClient mLocationClient;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash);
		ButterKnife.inject(this);

		mLocationClient = AppController.getInstance()
				.getLocationClientInstance();
		mLocationClient.registerLocationListener(this);
		mLocationClient.start();

		// new Handler().postDelayed(new Runnable() {
		//
		// @Override
		// public void run() {
		// //startMainAcitivty();
		// }
		// }, 200 * 1);
	}

	private void startMainAcitivty() {
		String userId = PreferencesUtils.getUserId();
		if (StringUtils.isBlank(userId)) {
			// 用户未登录
			startActivity(new Intent(SplashActivity.this, LoginActivity.class));
			finish();
		} else {
			startActivity(new Intent(SplashActivity.this, MainActivity.class));
			finish();
		}
	}

	@Override
	protected void onStop() {
		mLocationClient.unRegisterLocationListener(this);

		mLocationClient.stop();

		super.onStop();
	}

	@Override
	public void onReceiveLocation(BDLocation location) {

		AppController.getInstance().setLastDBLocation(location);
		startMainAcitivty();

		// StringBuffer sb = new StringBuffer(256);
		// sb.append("time : ");
		// sb.append(location.getTime());
		// sb.append("\nerror code : ");
		// sb.append(location.getLocType());
		// sb.append("\nlatitude : ");
		// sb.append(location.getLatitude());
		// sb.append("\nlontitude : ");
		// sb.append(location.getLongitude());
		// sb.append("\nradius : ");
		// sb.append(location.getRadius());
		// if (location.getLocType() == BDLocation.TypeGpsLocation) {
		// sb.append("\nspeed : ");
		// sb.append(location.getSpeed());
		// sb.append("\nsatellite : ");
		// sb.append(location.getSatelliteNumber());
		// sb.append("\ndirection : ");
		// sb.append("\naddr : ");
		// sb.append(location.getAddrStr());
		// sb.append(location.getDirection());
		// } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
		// sb.append("\naddr : ");
		// sb.append(location.getAddrStr());
		// // 运营商信息
		// sb.append("\noperationers : ");
		// sb.append(location.getOperators());
		// }
		//
		// Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();

		// Log.d(TAG, sb.toString());
	}
}
